package uk.ac.lkl.lpp.ui;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import java.util.Map;
import java.util.HashMap;

//import javax.swing.JButton;

import uk.ac.lkl.common.ui.ContextualisedViewCreator;
import uk.ac.lkl.lpp.PedagogyPlan;


import uk.ac.lkl.lpp.xml.ParameterMap;
import uk.ac.lkl.lpp.xml.XMLConstructor;
import uk.ac.lkl.lpp.xml.XMLException;


public class LinkingSetPanelXMLConstructor extends XMLConstructor {

    protected Object createFromParameterMap(PedagogyPlan plan,
                                            ParameterMap parameterMap)
            throws XMLException {

        try {
            HashMap<Class, ContextualisedViewCreator> viewCreatorMap = new HashMap<Class, ContextualisedViewCreator>();
            HashMap<String, String> viewMap = parameterMap.getMapParameter("viewClass");
            for (Map.Entry<String, String> entry : viewMap.entrySet()) {
                String className = entry.getKey();
                String viewCreatorClassName = entry.getValue() + "ViewCreator";
                Class cl = Class.forName(className);
                Class viewCreatorClass = Class.forName(viewCreatorClassName);
                Constructor constructor = viewCreatorClass.getConstructor(PedagogyPlan.class);
                ContextualisedViewCreator viewCreator = (ContextualisedViewCreator) constructor.newInstance(plan);
                viewCreatorMap.put(cl, viewCreator);
            }
            LinkingSetPanel linkingSetPanel = new LinkingSetPanel(plan,
                                                                  viewCreatorMap);
            return linkingSetPanel;
        }
        catch (InvocationTargetException e) {
            throw new XMLException(e);
        }
        catch (NoSuchMethodException e) {
            throw new XMLException(e);
        }
        catch (InstantiationException e) {
            throw new XMLException(e);
        }
        catch (IllegalAccessException e) {
            throw new XMLException(e);
        }
        catch (ClassCastException e) {
            throw new XMLException(e);
        }
        catch (ClassNotFoundException e) {
            throw new XMLException(e);
        }
    }


    protected void getParametersFromData(PedagogyPlan plan,
                                         ParameterMap parameterMap) {
        // todo
    }
}
